<?php
/**
 * 采集类
 * @author CallMeZ
 *
 */
class PageCollectController extends Controller{
	public function actionIndex(){
		require 'data\naruto\naruto.php';
		foreach($list as $k){
			echo '<a href="'.$k['url']. '">' . $k['title'] . '_' . $k['page'] . '</a><br>';
		}
	}

	public function actionNaruto(){
		$dirName = Yii::getPathOfAlias('webroot.data.naruto');
		if(empty($_GET['url']))
			exit('没有url不能采集哟');
		
		$url = $_GET['url'];
				
		header('Content-Type: text/html; charset=GBk');
		
		
		$html = Pager::getHtmlFile($url);
		if($a = $html->find('li[id=page__next]', 0)->find('a', 0)){
			$next = $a->attr['href'];
		}else{
			$input = $html->find('input[id=lastArtLinkUrl]', 0);
			$next = $input->attr['value'];
		}
		$totalPage = $html->find('select[id=p__select]',0)->last_child()->innertext;
		$page = $html->find('option[selected]', 0)->innertext;
		$title = $html->find('h2[class=t]', 0)->innertext;
		$src = $html->find('div[id=photoView]', 0)->find('img', 0)->attr['src'];
		
		
		$img = pathinfo($src);
		$localDir = $dirName . '/' . $title . '/';
		$localPath = $localDir . $page . '.' . $img['extension'];
		
 		$filename = $dirName . '/link.php';
 		$f = fopen($filename, 'a+');
 		fwrite($f, "array( 'url' => '$src', 'title' => '$title', 'page' => '$page', 'totalPage' => '$totalPage', 'localPath' => '$title/$page.$img[extension]'),\n");
 		fclose($f);
 		echo $title .'|' . $page . '|' . '->complete, Redirect to : ' . $next;
 		echo "<script>window.location.href='".$this->createUrl('', array('url' => $next))."';</script>";
	}
}
?>